/**
 * 
 */
package org.mtdownload.utils;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.prefs.Preferences;

/**
 * Utilities class
 * @author sergiu
 */
public class PreferencesManager {

	private static Preferences prefs;
	
	public static final String FTP_TYPE_ASCII="ASCII";
	public static final String FTP_TYPE_BINARY="Binary";
	
	public static final String PROGRESS_TYPE_BAR="Bar";
	public static final String PROGRESS_TYPE_BAR_COLORS="Bar (Colors)";
	public static final String PROGRESS_TYPE_PIE_COLORS="Pie (Colors)";
	public static final String PROGRESS_TYPE_PIE="Pie";
	public static final String PROGRESS_TYPE_NONE="None";
	
	public static Preferences getPreferences(){
		if(prefs==null){
			prefs=Preferences.userRoot();
		}
		return prefs;
	}
	
	public static void savePreferences(){
		try {			
			getPreferences().flush();	
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public static Dimension getFrameSize(){
		int width=getPreferences().getInt("mtdownload.framesize.width",600);
		int height=getPreferences().getInt("mtdownload.framesize.height",400);
		return new Dimension(width,height);
	}

	public static Point getFrameLocation(){
		int x=getPreferences().getInt("mtdownload.framelocation.x",-1);
		int y=getPreferences().getInt("mtdownload.framelocation.y",-1);
		if(x<0 || y<0){
			return null;
		}
		return new Point(x,y);
	}

	public static void setFrameLocation(Point p){
		getPreferences().putInt("mtdownload.framelocation.x",p.x);
		getPreferences().putInt("mtdownload.framelocation.y",p.y);
	}
	
	public static void setFrameSize(Dimension size){
		getPreferences().putInt("mtdownload.framesize.width",size.width);
		getPreferences().putInt("mtdownload.framesize.height",size.height);
	}
	public static int getSplitDividerLocation(){
		return getPreferences().getInt("mtdownload.splitdivider.location",100);
	}
	public static void setSplitDividerLocation(int location){
		getPreferences().putInt("mtdownload.splitdivider.location",location);
	}
	public static void setAnonymousFTPPassword(String password){
		getPreferences().put("mtdownload.ftp.anonymouspassword",password);
	}	
	public static String getAnonymousFTPPassword(){
		return getPreferences().get("mtdownload.ftp.anonymouspassword","anonymous@mtdownload.org");
	}

	public static void setFTPTransferType(String transferType){
		getPreferences().put("mtdownload.ftp.transfertype",transferType);
	}	
	public static String getFTPTransferType(){
		return getPreferences().get("mtdownload.ftp.transfertype",FTP_TYPE_BINARY);
	}
	public static void setDefaultSaveDir(String defaultSaveDir){
		getPreferences().put("mtdownload.options.defaultsavedir",defaultSaveDir);
	}
	public static String getDefaultSaveDir(){
		String defaultSaveDir=System.getProperty("user.home")+File.separator+"downloads"+File.separator;
		return getPreferences().get("mtdownload.options.defaultsavedir",defaultSaveDir);
	}
	public static void setDownloadListDatabase(String file){
		getPreferences().put("mtdownload.options.downloadlistdatabase",file);
	}
	public static String getDownloadListDatabase(){
		String downloadListDatabase=System.getProperty("user.home")+File.separator+".java"+File.separator+"mtdownload.db";
		return getPreferences().get("mtdownload.options.downloadlistdatabase",downloadListDatabase);
	}
	public static int getDefaultThreadsNumber(){
		return getPreferences().getInt("mtdownload.options.defaultthreadsnumber",5);
	}
	public static void setDefaultThreadsNumber(int nr){
		getPreferences().putInt("mtdownload.options.defaultthreadsnumber",nr);
	}
	public static int getBlockBytes(){
		return getPreferences().getInt("mtdownload.options.blockbytes",1024*10);
	}
	public static void setBlockBytes(int bytes){
		getPreferences().putInt("mtdownload.options.blockbytes",bytes);
	}
	public static void setProgressType(String type){
		getPreferences().put("mtdownload.options.progresstype",type);
	}	
	public static String getProgressType(){
		return getPreferences().get("mtdownload.options.progresstype",PROGRESS_TYPE_BAR);
	}

	public static void setProgressSingleColor(Color color){
		getPreferences().putInt("mtdownload.options.progresssinglecolor",color.getRGB());
	}	
	public static Color getProgressSingleColor(){
		//blue by default
		return new Color(getPreferences().getInt("mtdownload.options.progresssinglecolor",-16776961));
	}

	public static void setProgressBackgroundColor(Color color){
		getPreferences().putInt("mtdownload.options.progressbackgroundcolor",color.getRGB());
	}	
	public static Color getProgressBackgroundColor(){
		//white by default
		return new Color(getPreferences().getInt("mtdownload.options.progressbackgroundcolor",-1));
	}

	public static void setProgressTextColor(Color color){
		getPreferences().putInt("mtdownload.options.progresstextcolor",color.getRGB());
	}	
	public static Color getProgressTextColor(){
		//red by default
		return new Color(getPreferences().getInt("mtdownload.options.progresstextcolor",-65536));
	}
	
	public static List<Color> getProgressColorsList(){
		List<Color> l=new ArrayList<Color>();
		String defaultColorList="-16776961,-16711681,-65281,-16711936,-256,-14336,-20561,-65536,-16777216,-12566464,-8355712,-4144960";
		StringTokenizer tokenizer=new StringTokenizer(getPreferences().get("mtdownload.options.progresscolors",defaultColorList),",");
		while(tokenizer.hasMoreTokens()){
			String token=tokenizer.nextToken();
			l.add(new Color(Integer.parseInt(token)));
		}
		return l;
	}
	public static void setProgressColorsList(List<Color> l){
		StringBuffer buf=new StringBuffer();
		for(int i=0;i<l.size()-1;i++){
			buf.append(l.get(i).getRGB());
			buf.append(",");
		}
		if(l.size()>0){
			buf.append(l.get(l.size()-1).getRGB());
		}
		getPreferences().put("mtdownload.options.progresscolors",buf.toString());
	}
	
}
